/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Centrala;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import marcin.belzowski.uslugi.Administrator;

/**
 *
 * @author Marcin
 */
@Stateless
public class Admin implements AdminRemote {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("FirmaPU");
    EntityManager em = emf.createEntityManager();

    public List<Administrator> wyszukaj(String login,String haslo){
        Query q = em.createQuery("select d from Administrator d where d.login =?1 and d.haslo = ?2");
        q.setParameter(1, login);
        q.setParameter(2, haslo);

        return  q.getResultList();

    }
    public boolean loginIstnieje(String login){
        Query q = em.createQuery("select a from Administrator a where a.login = ?1");
        q.setParameter(1, login);
        if (q.getResultList().isEmpty())
            return false;
        else return true;
    }
    public void zapisz(Administrator admin){
        em.getTransaction().begin();
        em.persist(admin);
        em.getTransaction().commit();
    }
    public List<Administrator> wyszukajAdmin(String imie, String nazwisko){
        Query q = em.createQuery("select a from Administrator a where a.imie =?1 and a.nazwisko = ?2 ");
        q.setParameter(1, imie);
        q.setParameter(2, nazwisko);

        return q.getResultList();
    }

    public void usun(Administrator administrator) {
        Administrator a = em.getReference(Administrator.class,administrator.getId());
        a.setArchiwalny(true);
        em.getTransaction().begin();
        em.persist(a);
        em.getTransaction().commit();
    }
    public void odarchiwizuj(Administrator admin){
        Administrator a = em.getReference(Administrator.class,admin.getId());
        a.setArchiwalny(false);
        em.getTransaction().begin();
        em.persist(a);
        em.getTransaction().commit();
    }
}
